<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/4/8
 * Time: 15:27
 * editor: zhouzhou
 */

namespace app\bbs\api;

use app\admin\api\ConfigApi;

class FilterKeyWordsApi{

    const FILTER = "bbs/FilterKeyWordsApi/filter";
    const CHECK  = "bbs/FilterKeyWordsApi/check";

    /**
     * 关键词过滤
     */
    public function filter($posts,$replace='**'){

        $return = '';
        $result = apiCall(ConfigApi::GET_INFO,array(array('name'=>'FILTRATION_KEYWORDS')));
        if($result['status']){
            if(empty($result['info'])){
                $return = $posts;
            }else{
                $key_words = explode(',',$result['info']['value']);
                foreach($key_words as $val){
                    $val = trim(str_replace(array("/r/n", "/r", "/n"), "", $val));
                    if(is_array($posts)){
                        foreach ($posts as &$v) {
                            $v = str_replace($val,$replace,$v);
                        }
                    }else{
                        $posts = str_replace($val,$replace,$posts);
                    }
                }
            }
        }

        $return = $posts;
        return array('status'=>true,'info'=>$return);
    }
    /**
     * 检查是否有屏蔽的词汇
     * @param  [type] $posts [description]
     * @return boolean
     */
    public function check($posts){
        $return = '';
        $result = apiCall(ConfigApi::GET_INFO,array(array('name'=>'FILTRATION_KEYWORDS')));
        if($result['status']){
            if(!empty($result['info'])){
                $key_words = explode(',',$result['info']['value']);
                foreach($key_words as $val){
                   if(!empty($val) && strpos($posts, $val)) return false;
                }
            }
            return true;
        }else{
            return false;
        }
    }
}